package com.yuerlive.rainbow.module.live.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.netease.nimlib.sdk.chatroom.model.ChatRoomMember;
import com.yuerlive.rainbow.R;
import com.yuerlive.rainbow.utils.GlideUtil;
import com.yuerlive.rainbow.utils.L;
import com.yuerlive.rainbow.utils.Utils;

import java.util.List;

/**
 * Created by xiaoyi on 2017/5/25.
 */

public class MemberAdapter extends BaseAdapter {
    List<ChatRoomMember> mDatas;
    Context context;

    public MemberAdapter(List<ChatRoomMember> mDatas, Context context) {
        this.mDatas = mDatas;
        this.context = context;
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.layout_member_item, parent, false);
            holder = new ViewHolder();
            holder.imgHeader = (ImageView) convertView.findViewById(R.id.imgHeader);
            holder.tvLevel = (TextView) convertView.findViewById(R.id.tvLevel);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        GlideUtil.load(context, mDatas.get(position).getAvatar(), holder.imgHeader, R.mipmap.ic_launcher);
        if (mDatas.get(position).getExtension() != null && mDatas.get(position).getExtension().containsKey("level")) {
            holder.tvLevel.setText(mDatas.get(position).getExtension().get("level") + "");
            Utils.setLiveAudienceLevel(holder.tvLevel, (Integer) mDatas.get(position).getExtension().get("level"));
        }

        return convertView;
    }

    static class ViewHolder {
        ImageView imgHeader;
        TextView tvLevel;
    }
}
